var mongodb = require('./db');

function User (user) {
	this.name = user.name;
	this.password = user.password;
}

module.exports = User;

User.prototype.save = function save(callback) {
    var user = {
        name: this.name,
		_id : new Date(),
        password: this.password,
    };

	mongodb.open(function(err, db){
		if(err){
			return callback(err);
		} 

		db.collection('users', function(err, collection){
			if(err){
				mongodb.close();
				return callback(err);
			}
		  
			/*
			collection.remove('name',function(err,result){
               console.log(result);
          });*/
			collection.ensureIndex({'name':1,'_id':1},{w: 0});
			collection.insert(user,{safe: true}, function(err, user){
				mongodb.close();
				//console.log(err)
				//console.log(user)
				callback(err, user);
			});
		});
	});
};

User.get = function get(username, callback){
	mongodb.open(function(err, db){
		if(err){
			return callback(err);
		}
		
		db.collection('users', function(err, collection){
			if(err){
				mongodb.close();
				return callback(err);
			}
	
			collection.findOne({
				name: username
			},function(err, doc){
				mongodb.close();
				if(doc){
					var user = new User(doc);
					callback(err, user);
				} else {
					callback(err, null);
				}
			});
		});
	});
};